package com.fulihui.information.common.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;

/**
 * @author Willard.Hu on 2016/11/26 0026.
 */
@Configuration
public class SpringConfig {
    @Autowired
    Environment env;


    /**
     * Jedis 连接池配置类
     *
     * @return {@link JedisPoolConfig}
     */
    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(env.getProperty("redis.config.maxidle", Integer.class, 8));
        poolConfig.setMaxTotal(env.getProperty("redis.config.maxtotal", Integer.class, 8));
        return poolConfig;
    }

    /**
     * Jedis 连接池
     *
     * @param jedisPoolConfig {@link JedisPoolConfig}
     * @return {@link JedisPool}
     */
    @Bean
    public JedisPool jedisPool(JedisPoolConfig jedisPoolConfig) {
        return new JedisPool(jedisPoolConfig,
                env.getProperty("redis.pool.host", Protocol.DEFAULT_HOST),
                env.getProperty("redis.pool.port", Integer.class, Protocol.DEFAULT_PORT),
                env.getProperty("redis.pool.timeout", Integer.class, Protocol.DEFAULT_TIMEOUT),
                env.getProperty("redis.pool.password", (String) null));
    }


}